﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareMonkeys.LogClimber.Entities;

namespace SoftwareMonkeys.LogClimber.Controls
{
    public class LogTreeViewBuilder
    {
        private TreeView logTree;
        public TreeView LogTree
        {
            get { return logTree; }
            set { logTree = value; }
        }
        
        public string[] SelectedComponents = new String[]{};

        public LogTreeViewBuilder(TreeView logTree)
        {
            this.LogTree = logTree;
            SelectedComponents = null;
        }
        
        public LogTreeViewBuilder(TreeView logTree, string[] selectedComponents)
        {
            this.LogTree = logTree;
            this.SelectedComponents = selectedComponents;
        }

        public void Clear()
        {
            logTree.Nodes.Clear();
        }

        public void Show(Log log)
        {
            Clear();

            Append(log);
        }
        
        public void Show(Log[] logs)
        {
            Clear();

            Append(logs);
        }

        public void Append(Log[] logs)
        {
            foreach (Log log in logs)
            {
                Append(log);
            }
        }

        public void Append(Log log)
        {
            if (log == null)
                throw new ArgumentNullException("log");

            TreeNode logNode = new TreeNode();

            if (log.DateStamp != null && log.DateStamp != String.Empty)
                logNode.Text = log.DateStamp;
            else
                logNode.Text = "Log";

            logTree.Nodes.Add(logNode);

            if (log.ChildEntries != null)
            {
                foreach (Entry entry in log.ChildEntries)
                {
                    RenderEntry(logNode, entry);
                }
            }
        }

        private void RenderEntry(TreeNode parentNode, Entry entry)
        {
        	if (SelectedComponents == null || Array.IndexOf(SelectedComponents, entry.Component) > -1)
        	{
	            TreeNode entryNode = new TreeNode();
	            entryNode.Text = entry.Component + "." + entry.Method + ": " + entry.Data;
	            entryNode.Tag = entry;
	
	            parentNode.Nodes.Add(entryNode);

	            // Make the current node the parent to the child nodes
	            parentNode = entryNode;
        	}
        	
            if (entry.ChildEntries != null)
            {
                foreach (Entry childEntry in entry.ChildEntries)
                {
                    RenderEntry(parentNode, childEntry);
                }
            }
        }
        
        private bool IsEntryVisible(Entry entry)
        {
        	bool isVisible = Array.IndexOf(SelectedComponents, entry.Component) > -1;
        	System.Diagnostics.Debugger.Break();
        	return isVisible;
        }
    }
}
